/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.gwt.tests;

import com.google.gwt.junit.client.GWTTestCase;

import fr.openparts.OpenParts.client.OPCommandCallback;
import fr.openparts.OpenParts.client.OPCommandCallbackCaller;
import fr.openparts.OpenParts.client.OPCommandFailureReason;

/**
 * GWT tests for the OPCommandCallbackCaller.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 09 December 2010
 */
public class OPCommandCallbackCallerTestGwt extends GWTTestCase {

    private OPCommandCallback callback;
    private int onFailCalled = 0;
    private int onSuccessCalled = 0;

    /**
     * Return the name of the tested GWT Module.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
     */
    @Override
    public final String getModuleName() {
        return "fr.openparts.OpenParts.test.gwt.TestOpenParts";
    }

    /**
     * Called at the start of each test to set up the action.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtSetUp()
     */
    @Override
    protected void gwtSetUp() throws Exception {
        super.gwtSetUp();
        callback = new OPCommandCallback() {
            @Override
            public void onFail(final OPCommandFailureReason reason) {
                ++onFailCalled;
            }

            @Override
            public void onSuccess() {
                ++onSuccessCalled;
            }
        };
    }

    /**
     * Called at the end of each test to clean up the mess.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtTearDown()
     */
    @Override
    protected final void gwtTearDown() throws Exception {
        super.gwtTearDown();
        onFailCalled = 0;
        onSuccessCalled = 0;
    }

    /**
     * Test creating a callback caller. Should get something not null.
     */
    public final void testCreatingCallbackCaller() {
        assertNotNull("callbackCaller shouldn't be null", new OPCommandCallbackCaller(callback));
    }
    
    /**
     * Test creating a callback caller with a null callback. Should raise an assertion error.
     */
    public final void testCreatingCallbackCallerWithNullCallback() {
        if (assertionsAreEnabled()) {
            try {
                new OPCommandCallbackCaller(null);
                fail("OPCommandCallbackCaller should raise an assertion error when building with a null callback.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test calling the onSuccess method. Should call the callback onSuccess() method.
     */
    public final void testCallingOnSuccess() {
        final OPCommandCallbackCaller caller = new OPCommandCallbackCaller(callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        caller.onSuccess();
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test calling the onFail method. Should call the callback onFail() method.
     */
    public final void testCallingOnFail() {
        final OPCommandCallbackCaller caller = new OPCommandCallbackCaller(callback);
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        caller.onFail(OPCommandFailureReason.commandFailureReasonFactory(0, null));
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
    }

    /**
     * Check if assertions are enabled.
     * 
     * @return true if assertions are enabled, false otherwise
     */
    private final boolean assertionsAreEnabled() {
        try {
            assert false;
            return false;
        } catch (AssertionError e) {
            return true;
        }
    }
}
